const vm = require('vm');

module.exports.Sandbox = class {
    constructor({ sc, ctx }) {
        try {
            this.script = new vm.Script(sc);
            this.ctx = vm.createContext(Object.assign({}, ctx));
        } catch (error) {
            console.error('Unexpected identifier in sc.');
        }
    }

    testScript() {
        if(!this.script) return false;
        try {
            let rtn = this.script.runInContext(this.ctx);
            if (rtn === false || rtn === true) return true;
            return false;
        } catch (err) {
            console.error('VM test Faild', err);
            return false;
        }
    }

    runScript() {
        if(!this.script) return false;
        try {
            return this.script.runInContext(this.ctx);
        } catch (err) {
            console.error('VM running Faild', err);
            return false;
        }
    }

}